package com.huawei.od;

/**
 * 最长子序列
 */
public class T5 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 2};
        int num = 4;
        System.out.println(method(arr, num));
    }

    private static int method(int[] arr, int num) {
        for (int i = arr.length; i > 0; i--) {
            if (lengthMeet(arr, i, num)) {
                return i;
            }
        }
        return -1;
    }

    private static boolean lengthMeet(int[] arr, int length, int sum) {
        for (int i = 0; i < arr.length - length + 1; i++) {
            if (getSum(i, arr, length) == sum) {
                return true;
            }
        }
        return false;
    }

    private static int getSum(int i, int[] arr, int length) {
        int sum = 0;
        for (int j = 0; j < arr.length; j++) {
            if (j >= i && j < i + length) {
                sum = sum + arr[j];
            }
        }
        return sum;
    }
}
